package com.accloud.cloudservice;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import com.accloud.utils.LogUtil;
import io.reactivex.annotations.SchedulerSupport;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.commons.codec.binary.Hex;
import org.json.JSONException;
import org.json.JSONObject;

/* access modifiers changed from: package-private */
public class ACEventManager {
    private static final String TAG = "ACEventManager";
    private static final SimpleDateFormat ZONED_DATE_FORMATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'000'Z");
    private JSONObject mProperties;

    ACEventManager(Context context) {

        this.mProperties = new JSONObject();
        try {
            this.mProperties.put("pid", AC.phoneId);
            JSONObject mobile = new JSONObject();
            mobile.put("model", Build.MODEL);
            mobile.put("os", "Android " + Build.VERSION.RELEASE);
            this.mProperties.put("mobile", mobile);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private JSONObject copyProperties() throws JSONException {
        List<String> keys = new ArrayList<>();
        Iterator<String> iterator = this.mProperties.keys();
        while (iterator.hasNext()) {
            keys.add(iterator.next());
        }
        return new JSONObject(this.mProperties, (String[]) keys.toArray(new String[keys.size()]));
    }

    /* access modifiers changed from: package-private */
    public void track(ACEvent event) {
        String network;
        try {
            JSONObject properties = copyProperties();
            properties.put("trace_id", randomHex(32));
            properties.put("data", event.getData());
            JSONObject eventTime = new JSONObject();
            eventTime.put("timestamp", ZONED_DATE_FORMATE.format(new Date()));
            properties.put("event", eventTime);
            properties.put("result", event.result);
            NetworkInfo activeNetworkInfo = ((ConnectivityManager) AC.context.getSystemService("connectivity")).getActiveNetworkInfo();
            if (activeNetworkInfo == null) {
                network = SchedulerSupport.NONE;
            } else {
                network = activeNetworkInfo.getType() == 1 ? "wifi" : "cellular";
            }
            properties.getJSONObject("mobile").put("network", network);
            if (event.subDomain != null) {
                properties.put("sub_domain", event.subDomain);
            }
            if (event.physicalDeviceId != null) {
                properties.put("physical_id", event.physicalDeviceId);
            }
            if (event.deviceId != null) {
                properties.put("device_id", String.valueOf(event.deviceId));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String randomHex(int length) {
        byte[] bytes = new byte[(length / 2)];
        new Random().nextBytes(bytes);
        return new String(Hex.encodeHex(bytes));
    }
}
